<?php

namespace App\Trip\Notify\Sms\Messages\Train;

use App\Models\Train\TrainOrderPassengers\TrainOrderPassenger;
use App\Models\Train\TrainOrders\TrainOrder;
use App\Trip\Train\Seat\TrainPassengerSeat;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Message;

class ChangeSuccessNotifyMessage extends Message
{
    //kepter 改签成功
    private TrainOrder           $order;
    private ?TrainOrderPassenger $passenger;

    public function __construct(TrainOrder $order, ?TrainOrderPassenger $passenger = null)
    {
        $this->order     = $order;
        $this->passenger = $passenger;
    }

    // 定义直接使用内容发送平台的内容
    public function getContent(GatewayInterface $gateway = null)
    {
        return '';
    }

    // 定义使用模板发送方式平台所需要的模板 ID
    public function getTemplate(GatewayInterface $gateway = null)
    {
        return '1499611';
    }

    // 模板参数
    public function getData(GatewayInterface $gateway = null)
    {
        $depart_at = $this->order->getTrainDate();
        $train     = $this->order->getMainTrain();
        if (!$this->passenger) {
            $passengers = $this->order->passengers;
            $seat_desc  = collect($passengers)->map(function ($passenger) {
                return "{$passenger->getTrueName()}".TrainPassengerSeat::seatNoDesc($passenger);
            })->implode(',');
        } else {
            $seat      = TrainPassengerSeat::seatNoDesc($this->passenger);
            $seat_desc = "{$this->passenger->getTrueName()}{$seat}";
        }


        return [
            1 => Carbon::parse($depart_at)->month,
            2 => Carbon::parse($depart_at)->day,
            3 => Carbon::parse($depart_at)->format("H:i"),
            4 => " ".$this->order->getTrainCode(),
            5 => Str::limit(" {$train->getFromNameUy()}->{$train->getToNameUy()} ", 80, ""),
            6 => $seat_desc,
        ];
    }
}